﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class FloatingCountedConcurrentSessionsStrategyTests
    {
        private License license;

        [TestInitialize]
        public void Setup()
        {
            license = new CountedFloatingLicense { Client = LicenseClientType.Machine, LimitType = FloatingLicenseLimitType.Concurrency, Limit = 1};
        }

        [TestMethod]
        public void Should_add_session_to_client_sessions()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new FloatingCountedConcurrentSessionsStrategy(license, LicenseEntitlementValidity.Null, fik);
            var sessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection(), ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());

            Assert.AreEqual(1, strategy.GetClientSessions().Count());
            Assert.IsTrue(strategy.HasClientSession(sessionId));
        }

        [TestMethod]
        [ExpectedException(typeof(CountedFloatingLicensesExhaustedException))]
        public void Should_not_add_session_to_client_sessions_if_limit_exceeded()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new FloatingCountedConcurrentSessionsStrategy(license, LicenseEntitlementValidity.Null, fik);
            
            strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection(), ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
            strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection{new ProcessorIdentifier{Value = "SomeProcessor"}}, ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
        }

        
        [TestMethod]
        public void Should_not_add_session__to_client_sessions_or_check_limit_if_client_session_already_exists()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new FloatingCountedConcurrentSessionsStrategy(license, LicenseEntitlementValidity.Null, fik);
            var identifierCollection = new IdentifierCollection();
            var sessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = identifierCollection, ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
            var returnedSessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = identifierCollection, ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());

            Assert.AreEqual(1, strategy.GetClientSessions().Count());
            Assert.AreEqual(sessionId, returnedSessionId);
        }
    }
}